package daoServlet;

//import cn.smbms.pojo.Role;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.IOUtils;
import daoBean.*;
//import cn.smbms.service.role.RoleService;
//import cn.smbms.service.role.RoleServiceImpl;
import daoService.UserService;
import daoService.UserServiceImpl;
//import cn.smbms.service.user.UserServiceImpl;
import org.junit.jupiter.api.Test;
import utils.Constants;
//import cn.smbms.tools.PageSupport;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(name = "UserServlet",urlPatterns = "/us")
public class UserServlet extends HttpServlet {
	public UserServlet() {
		super();
	}
	@Override
	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		String id = "1";
//		UserServiceImpl userService = new UserServiceImpl();
//		User user = userService.getUserById(id);
//		userService.getUserById(id);
		String type = request.getParameter("type");
		System.out.println("get请求的type:"+type);
		if (type.equals("getUserById")){
			this.getUserById(request,response);
		}
//		userService.getUserById(id);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		modify
		try {
			// 获取了json数据
			String paramJson = IOUtils.toString(request.getInputStream());
			JSONObject jsonObject = JSONObject.parseObject(paramJson);
			String type  = jsonObject.getString("type");
			System.out.println("post请求的type:"+type);

			if (type.equals("modify")){
				this.modify(request,response);
			}

			if (type.equals("addUser")){
				this.addUser(request,response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	// 测试根据用户id 查询用户信息
	@Test
	public void testGetUserById(){
		String id = "2";
		UserService userService = new UserServiceImpl();
		//User user = userService.getUserById(id);
		User user = userService.getUserById(id);
		if (user!=null){
			System.out.println("返回的用户信息"+user.toString());
			// 转json数据(),true规范格式，但是没有 保留空字段  JSON.toJSONString
			System.out.println(JSON.toJSONString(user,true));
		}else{
			System.out.println("没找到用户");
		}
//		userService.getUserById(id);
	}


/*	private void updatePwd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Object o = request.getSession().getAttribute(Constants.USER_SESSION);
		String newpassword = request.getParameter("newpassword");
		boolean flag = false;
		if(o != null && !StringUtils.isNullOrEmpty(newpassword)){
			UserService userService = new UserServiceImpl();
			flag = userService.updatePwd(((User)o).getId(),newpassword);
			if(flag){
				request.setAttribute(Constants.SYS_MESSAGE, "修改密码成功,请退出并使用新密码重新登录！");
				request.getSession().removeAttribute(Constants.USER_SESSION);//session注销
			}else{
				request.setAttribute(Constants.SYS_MESSAGE, "修改密码失败！");
			}
		}else{
			request.setAttribute(Constants.SYS_MESSAGE, "修改密码失败！");
		}
		request.getRequestDispatcher("pwdmodify.jsp").forward(request, response);
	}*/
	
	/*private void getPwdByUserId(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object o = request.getSession().getAttribute(Constants.USER_SESSION);
		String oldpassword = request.getParameter("oldpassword");
		Map<String, String> resultMap = new HashMap<String, String>();
		
		if(null == o ){//session过期
			resultMap.put("result", "sessionerror");
		}else if(StringUtils.isNullOrEmpty(oldpassword)){//旧密码输入为空
			resultMap.put("result", "error");
		}else{
			String sessionPwd = ((User)o).getUserPassword();
			if(oldpassword.equals(sessionPwd)){
				resultMap.put("result", "true");
			}else{//旧密码输入不正确
				resultMap.put("result", "false");
			}
		}
		
		response.setContentType("application/json");
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.write(JSONArray.toJSONString(resultMap));
		outPrintWriter.flush();
		outPrintWriter.close();
	}
	
	*/

	private void addUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// SQL报错
		String id = "3";
		String phone ="13688889990";
		String userName = "添加的用户";
		String userPassword = "12345678";
		String address = "ww的地址22";
		User user = new User();
		user.setId(Integer.valueOf(id));
		user.setUserName(userName);
		user.setAddress(address);
		user.setUserPassword(userPassword);
		user.setPhone(phone);

		UserService userService = new UserServiceImpl();
		int res = userService.addUser(user);

		if(res>0){
			//response.sendRedirect(request.getContextPath()+"/jsp/user.do?method=query");
			System.out.println("添加成功："+res); // 1为成功
		}else{
			//request.getRequestDispatcher("usermodify.jsp").forward(request, response);
			System.out.println("添加失败了："+res);
		}

	}
	// 修改一个用户信息 SUCCESS
	private void modify(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*	String id = request.getParameter("uid");*/

		// SQL报错
		String id = "18";
		String phone ="13688889990";
		String userName = "系统管理员";
		String userPassword = "12345678";
		String address = "ww的地址22";
		User user = new User();
		user.setId(Integer.valueOf(id));
		user.setUserName(userName);
		user.setAddress(address);
		user.setUserPassword(userPassword);
		user.setPhone(phone);

		UserService userService = new UserServiceImpl();
		int res = userService.modify(user);

		if(res>0){
			//response.sendRedirect(request.getContextPath()+"/jsp/user.do?method=query");
			System.out.println("修改的结果："+res); // 1为成功
		}else{
			//request.getRequestDispatcher("usermodify.jsp").forward(request, response);
			System.out.println("失败了："+res);
		}


	}
	// 查询一个用户信息 success
	private void getUserById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

			System.out.println("getUserById");
			String id = "1";
			UserServiceImpl userService = new UserServiceImpl();
			User user = userService.getUserById(id);
		    System.out.println("返回的用户信息"+user.toString());
			if (user!=null){
				System.out.println(JSON.toJSONString(user)); // 转json数据
			}else{
				System.out.println("没找到用户");
			}
			response.getWriter().write(JSON.toJSONString(user));
			response.getWriter().flush();
			response.getWriter().close();
	}
	
	/*private void delUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("uid");
		Integer delId = 0;
		try{
			delId = Integer.parseInt(id);
		}catch (Exception e) {
			// TODO: handle exception
			delId = 0;
		}
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(delId <= 0){
			resultMap.put("delResult", "notexist");
		}else{
			UserService userService = new UserServiceImpl();
			if(userService.deleteUserById(delId)){
				resultMap.put("delResult", "true");
			}else{
				resultMap.put("delResult", "false");
			}
		}
		
		//把resultMap转换成json对象输出
		response.setContentType("application/json");
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.write(JSONArray.toJSONString(resultMap));
		outPrintWriter.flush();
		outPrintWriter.close();
	}*/

/*	private void userCodeExist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//判断用户账号是否可用
		String userCode = request.getParameter("userCode");
		
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(StringUtils.isNullOrEmpty(userCode)){
			//userCode == null || userCode.equals("")
			resultMap.put("userCode", "exist");
		}else{
			UserService userService = new UserServiceImpl();
			User user = userService.selectUserCodeExist(userCode);
			if(null != user){
				resultMap.put("userCode","exist");
			}else{
				resultMap.put("userCode", "notexist");
			}
		}
		
		//把resultMap转为json字符串以json的形式输出
		//配置上下文的输出类型
		response.setContentType("application/json");
		//从response对象中获取往外输出的writer对象
		PrintWriter outPrintWriter = response.getWriter();
		//把resultMap转为json字符串 输出
		outPrintWriter.write(JSONArray.toJSONString(resultMap));
		outPrintWriter.flush();//刷新
		outPrintWriter.close();//关闭流
	}
	
	private void getRoleList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Role> roleList = null;
		RoleService roleService = new RoleServiceImpl();
		roleList = roleService.getRoleList();
		//把roleList转换成json对象输出
		response.setContentType("application/json");
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.write(JSONArray.toJSONString(roleList));
		outPrintWriter.flush();
		outPrintWriter.close();
	}
	
	private void query(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//查询用户列表

		//从前端获取数据
		String queryUserName = request.getParameter("queryname");
		String temp = request.getParameter("queryUserRole");
		String pageIndex = request.getParameter("pageIndex");
		int queryUserRole = 0;

		//获取用户列表

		UserService userService = new UserServiceImpl();

		//第一次走页面一定是第一页,页面大小固定的
		List<User> userList = null;
		//设置页面容量
    	int pageSize = Constants.pageSize;
    	//当前页码
    	int currentPageNo = 1;
		*//**
		 * http://localhost:8090/SMBMS/userlist.do
		 * ----queryUserName --NULL
		 * http://localhost:8090/SMBMS/userlist.do?queryname=
		 * --queryUserName ---""
		 *//*
		System.out.println("queryUserName servlet--------"+queryUserName);  
		System.out.println("queryUserRole servlet--------"+queryUserRole);  
		System.out.println("query pageIndex--------- > " + pageIndex);
		if(queryUserName == null){
			queryUserName = "";
		}
		if(temp != null && !temp.equals("")){
			queryUserRole = Integer.parseInt(temp);//给查询赋值
		}
		
    	if(pageIndex != null){
    		try{
    			currentPageNo = Integer.valueOf(pageIndex);
    		}catch(NumberFormatException e){
    			response.sendRedirect("error.jsp");
    		}
    	}	
    	//总数量（表）	
    	int totalCount	= userService.getUserCount(queryUserName,queryUserRole);
    	//总页数
    	PageSupport pages=new PageSupport();

    	pages.setCurrentPageNo(currentPageNo);

    	pages.setPageSize(pageSize);

    	pages.setTotalCount(totalCount);
    	
    	int totalPageCount = pages.getTotalPageCount();
    	
    	//控制首页和尾页
    	if(currentPageNo < 1){
    		currentPageNo = 1;
    	}else if(currentPageNo > totalPageCount){
    		currentPageNo = totalPageCount;
    	}
		
		
		userList = userService.getUserList(queryUserName,queryUserRole,currentPageNo, pageSize);
		request.setAttribute("userList", userList);
		List<Role> roleList = null;
		RoleService roleService = new RoleServiceImpl();
		roleList = roleService.getRoleList();
		request.setAttribute("roleList", roleList);
		request.setAttribute("queryUserName", queryUserName);
		request.setAttribute("queryUserRole", queryUserRole);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("totalCount", totalCount);
		request.setAttribute("currentPageNo", currentPageNo);
		request.getRequestDispatcher("userlist.jsp").forward(request, response);
	}
	
*/

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
